package com.ruoyi.system.apiController;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.YyAppointment;
import com.ruoyi.system.domain.YyAppointmentTime;
import com.ruoyi.system.service.IYyAppointmentService;
import com.ruoyi.system.service.IYyAppointmentTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

//预约接口

@RestController
@RequestMapping("/api/appointment")
public class YyAppointmentApiController {

    @Autowired
    private IYyAppointmentService yyAppointmentService;
    @Autowired
    private IYyAppointmentTimeService yyAppointmentTimeService;

    /**
     * 查询今天哪个时间端还能预约
     */
    @GetMapping("/appointmentTime")
    public AjaxResult appointmentTime(Integer doctorId, Integer departmentId) {

        List<Map<String, Object>> list =  yyAppointmentTimeService.selectAppointmentTime(doctorId,departmentId);
        return AjaxResult.success(list);
    }


    /**
     * 查询某个医生哪个时间段还能预约
     */
    @GetMapping("/appointmentTime2")
    public AjaxResult appointmentTime2(Long doctorId, Integer departmentId,String date) {

        List<Map<String, Object>> list =  yyAppointmentTimeService.selectAppointmentTime2(doctorId,departmentId,date);
        return AjaxResult.success(list);
    }


    /**
     * 患者预约
     */
    @PostMapping("/add")
    public AjaxResult add(
                          @RequestParam(name = "patientId",required = false) Long patientId,
                          @RequestParam(name = "doctorId",required = false) Long doctorId,
                          @RequestParam(name = "departmentId",required = false) Integer departmentId,
                          @RequestParam(name = "appointmentTime",required = false) String appointmentTime,
                          @RequestParam(name = "yyTime",required = false) String yyTime
    ) throws ParseException {
        YyAppointment yyAppointment = new YyAppointment();
        yyAppointment.setPatientId(patientId);
        yyAppointment.setDoctorId(doctorId);
        yyAppointment.setDepartmentId(departmentId);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (appointmentTime != null){
            Date parse = format.parse(appointmentTime);
            yyAppointment.setAppointmentTime(parse);
        }
        yyAppointment.setYyTime(yyTime);


        int i = yyAppointmentService.insertYyAppointment2(yyAppointment);
        if (i < 1){
            return AjaxResult.error("预约失败");
        }
        return AjaxResult.success("预约成功");
    }


    /**
     * 查看所有预约
     */
    @GetMapping("/list")
    public AjaxResult list(YyAppointment yyAppointment) {
        List<YyAppointment> list = yyAppointmentService.selectAllYyAppointmentList(yyAppointment);
        return AjaxResult.success(list);
    }

    /**
     * 预约审核
     */
    @PostMapping("/check")
    public AjaxResult check(
                            @RequestParam(name = "status",required = false) String status,
                            @RequestParam(name = "appointmentId",required = false) Long appointmentId) {
        YyAppointment yyAppointment = new YyAppointment();
        yyAppointment.setAppointmentId(appointmentId);
        yyAppointment.setStatus(status);
        int i = yyAppointmentService.updateYyAppointment(yyAppointment);
        if (i < 1){
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success("修改成功");
    }
}
